//获取应用实例
var app = getApp()
var common = require('../../template/getCode.js')
var showAlert = require('../../../utils/showAlert.js')
var courierHelper = require('../../../utils/courier.js')
var util = require('../../../utils/util.js');
var Bmob = require('../../../utils/Bmob-1.6.5.min.js');
var that;
var myDate = new Date();
//格式化日期
function formate_data(myDate) {
  let month_add = myDate.getMonth() + 1;
  var formate_result = myDate.getFullYear() + '-' +
    month_add + '-' +
    myDate.getDate()
  return formate_result;
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    myUserInfo: [],
    products: []
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    that.setData({ //初始化数据
      src: "",
      isSrc: false,
      ishide: "0",
      autoFocus: true,
      isLoading: false,
      loading: true,
      isdisabled: false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.hideToast();

    console.log('onReady');



  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    console.log('onShow');

    var myUserInfo = wx.getStorageSync("myUserInfo");

    if (!myUserInfo) {
      const diary = Bmob.Query("userInfos");

      diary.equalTo("openid", "==", wx.getStorageSync("user_openid"));

      diary.find().then(res => {
        if (res.length == 0) {

          diary.set("openid", wx.getStorageSync("user_openid"));
          diary.set("CNName", '');
          diary.set("Mobile", '');
          diary.set("ShopNo", '');
          diary.save();

        }

      }).catch(err => {
        console.log(err)

      })
    }

    that.setData({
      myUserInfo: myUserInfo
    });
  },



  //提交表单
  submitForm: function (e) {

    that.setData({
      isLoading: true,
      isdisabled: true
    })
    var userName = e.detail.value.CNName;
    var userMobile = e.detail.value.Mobile;
    var userShopNo = e.detail.value.ShopNo;


    const diary = Bmob.Query("userInfos");

    diary.equalTo("openid", "==", wx.getStorageSync("user_openid"));

    diary.find().then(res => {
      var item = res[0];

      diary.get(item.objectId).then(res => {
        console.log(res)
        res.set("CNName", userName);
        res.set("Mobile", userMobile);
        res.set("ShopNo", userShopNo);
        res.save();
        //添加成功，返回成功之后的objectId(注意，返回的属性名字是id,而不是objectId)
        common.dataLoading("绑定成功", "success", function () {

          wx.setStorageSync("myUserInfo", {
            CNName: userName,
            Mobile: userMobile,
            ShopNo: userShopNo
          });
          that.setData({
            isLoading: false,
            isdisabled: false,
            myUserInfo: {
              CNName: userName,
              Mobile: userMobile,
              ShopNo: userShopNo
            }
          })
        });
      }).catch(err => {
        console.log(err)
        common.dataLoading("绑定失败", "loading");
        that.setData({
          isLoading: false,
          isdisabled: false
        })
      })

    });








  },
  //提交表单 商品
  submitFormSHop: function (e) {
    console.log(e.detail.value);


  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  courierTap: function () {
    that.setData({
      isLoading: true,
      isdisabled: true
    })
    if (this.changeByOpenid(wx.getStorageSync("user_openid"))) {
      wx.navigateTo({
        url: '/pages/courier/config/config',
      })
    }
    else {
      showAlert.showModal("你不是快递员", "你要申请吗!", function () {
        console.log('我要申请');

        //检查是否申请
        if (!courierHelper.isRequestCourier()) {
          const query = Bmob.Query('couriers');


          query.set("openid", wx.getStorageSync("user_openid"));
          query.set("myType", 0);
          query.set("myInfo", "我要申请");
          query.save().then(res => {
            showAlert.showTip("申请完成");
            console.log(`申请完成`)
          });
        }




      }, true, '我要申请');
      that.setData({
        isLoading: false,
        isdisabled: false
      })
      //common.dataLoading("你不是快递员,请联系管理员配置!", "loading");

    }

  },
  changeByOpenid: function (openid) {
    that.setData({
      isLoading: true,
      isdisabled: true
    })
    return courierHelper.isCourier();

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})



//根据活动类型获取活动类型名称
function getTypeName(acttype) {
  var acttypeName = "";
  if (acttype == 1) acttypeName = "运动";
  else if (acttype == 2) acttypeName = "游戏";
  else if (acttype == 3) acttypeName = "交友";
  else if (acttype == 4) acttypeName = "旅行";
  else if (acttype == 5) acttypeName = "读书";
  else if (acttype == 6) acttypeName = "竞赛";
  else if (acttype == 7) acttypeName = "电影";
  else if (acttype == 8) acttypeName = "音乐";
  else if (acttype == 9) acttypeName = "其他";
  return acttypeName;
}